#!/bin/sh

bdir=`dirname $0`
. $bdir/../src/libinclude.sh

get_spaces () {
    local spaces
    spaces="                              "
    echo "$spaces" | cut -c 1-$(( ${#spaces} - ${#1} ))
}

test_get_vars () {
    for var in "$@"; do
        if test_is_variable_set $var; then
            eval 'echo $var $'$var
        else
            echo $var
        fi
    done
}

test_is_variable_set () {
    include_get_set_vars | grep -e "\b$1\b" 2>/dev/null >/dev/null
    return $?
}


#include_test_variable () {
    #for var in "$@"; do
        #include_is_variable_set "$var" || return 1
    #done
#}



test_vars () {
    local sp0 sp1 pre
    [ -n "$1" ] && pre=${1}_ ; shift
    test_get_vars `echo "$@" | sed -e "s/[[:alnum:]_]*/${pre}&/g"` |\
    while read var val; do
        sp0="`get_spaces $var`"
        sp1="`get_spaces $val`"
        echo -n "\$$var" "$sp0" ":" "$val" "$sp1" ": "
        [ -n "$val" ] && echo "ok" || echo "ERROR"
    done
    unset "$@" 2>/dev/null
}

test_funcs () {
    local sp0 sp1 pre
    [ -n "$1" ] && pre=${1}_ ; shift
    for func in `echo "$@" | sed -e "s/[[:alnum:]_]*/${pre}&/g"`; do
        val=`$func 2>/dev/null`
        sp0="`get_spaces $func`"
        sp1="`get_spaces $val`"
        echo -n "$func " "$sp0" ":" "$val" "$sp1" ": "
        [ -n "$val" ] && echo "ok" || echo "ERROR"
    done
    unset -f "$@" 2>/dev/null
}


echo
echo 'DEPENDECIES'
echo '-----------------------------------------------------------------------------'
echo "with each test the dependencies 'grep' and 'ls' will be checked"
echo '-----------------------------------------------------------------------------'

echo 
echo 'TEST: include'
echo '============='
include testlib
echo 'name                             : value                           : status  '
echo '-----------------------------------------------------------------------------'
test_vars "" XXX pre_var pre_block_var
test_funcs "" abc pre_func pre_block_func
echo '-----------------------------------------------------------------------------'


echo 
echo 
echo 'TEST: import'
echo '============'
import testlib
echo 'name                             : value                           : status  '
echo '-----------------------------------------------------------------------------'
test_vars "testlib" XXX pre_var pre_block_var
test_funcs "testlib" abc pre_func pre_block_func
echo '-----------------------------------------------------------------------------'


echo 
echo 
echo 'TEST: import testlib as lib'
echo '==========================='
import testlib as lib
echo 'name                             : value                           : status  '
echo '-----------------------------------------------------------------------------'
test_vars "lib" XXX pre_var pre_block_var
test_funcs "lib" abc pre_func pre_block_func
echo '-----------------------------------------------------------------------------'


echo 
echo 
echo 'TEST: from testlib import XXX pre_block_func'
echo '============================================'
from testlib import XXX abc pre_var pre_block_func
echo 'name                             : value                           : status  '
echo '-----------------------------------------------------------------------------'
test_vars "" XXX 
test_funcs "" pre_block_func
echo '-----------------------------------------------------------------------------'
